﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Documents;
using System.Globalization;

namespace nl.tno.ict.multitouch.wpfmultitouch.controls
{
    public class KeyboardButtonAdorner : Adorner
    {
        #region Data

        private SolidColorBrush _brush;
        private int _buttonContentLength;

        #endregion // Data
        
        #region Constructor        
        
        public KeyboardButtonAdorner(FrameworkElement adornedElement)
            : base(adornedElement)
        {
            _brush = new SolidColorBrush(Colors.LightGray);
            _buttonContentLength = ((MTButton)AdornedElement).Content.ToString().Length;
        }
        #endregion // Constructor

        /// <summary>
        /// A common way to implement an adorner's rendering behavior is to override the OnRender
        /// method, which is called by the layout system as part of a rendering pass.
        /// </summary>
        /// <param name="drawingContext">The context to be drawn</param>
        protected override void OnRender(DrawingContext drawingContext)
        {            
            drawingContext.DrawRoundedRectangle(_brush, new Pen(Brushes.Black, 2), new Rect(AdornedElement.RenderSize.Width / 8, -AdornedElement.RenderSize.Height, (AdornedElement.RenderSize.Width / 8) * 6, AdornedElement.RenderSize.Height), 5, 5);
            drawingContext.DrawText(new FormattedText(((MTButton)AdornedElement).Content.ToString(), new CultureInfo("en-US"), FlowDirection.LeftToRight, new Typeface("Arial"), 15, Brushes.Black), new Point((AdornedElement.RenderSize.Width / 2) - (Math.Round(Convert.ToDouble(_buttonContentLength / 2), 0, MidpointRounding.AwayFromZero) * 9), -(AdornedElement.RenderSize.Height / 2) - 10));
        }
    }
}
